/*
 * @Author: ZhanXie
 * @Date: 2020-08-11 10:13:00
 * @LastEditors: ZhanXie
 * @LastEditTime: 2020-09-14 22:11:19
 * @Description: user api 路由
 * @FilePath: /weibo/src/routes/api/user.js
 */
const router = require('koa-router')()
// const jwt = require('jsonwebtoken')
// const util = require('util')
// const verify = util.promisify(jwt.verify)

// const { SECRET } = require('../../conf/const')

const {
  isExist,
  register,
  login,
  delCurUser,
  logout,
  changeInfo,
  changePassword
} = require('../../controller/user')
const { getFollowers } = require('../../controller/user-relation')
const userValidate = require('../../validator/user')
const { genValidator } = require('../../middlewares/validator')
const { loginCheck } = require('../../middlewares/loginChecks')
const { isTest } = require('../../utils/env')

router.prefix('/api/user')

/********* jwt demo start */
// router.post('/login', async (ctx, next) => {
//   const { userName, password } = ctx.request.body
//   // const token = jwt.sign(us)
//   let userInfo
//   if (userName === 'zhangsan' && password === '123') {
//     userInfo = {
//       userId: 1,
//       userName: 'zhangsan',
//       nickName: '张三'
//     }
//   }

//   // 加密 userInfo 
//   let token = ''
//   if (userInfo) {
//     token = jwt.sign(userInfo, SECRET, { expiresIn: '1h' }) // expirseIn 有效时长
//   }

//   if (userInfo == null) {
//     ctx.body = {
//       errno: -1,
//       msg: '登录失败'
//     }
//     return
//   }

//   ctx.body = {
//     errno: 0,
//     data: token
//   }
// })

// router.get('/getUserInfo', async (ctx, next) => {
//   // Authorization: Bearer token
//   const token = ctx.header.authorization
//   if (token) {
//     let payload
//     try {
//       payload = await verify(token.split(' ')[1], SECRET)
//       ctx.body = {
//         errno: 0,
//         userInfo: payload
//       }
//     } catch (error) {
//       console.log('token verify fail:', error)
//     }
//   }
// })

/********* jwt demo end */


// 注册路由
router.post('/register', genValidator(userValidate), async (ctx, next) => {
  const { userName, password, gender } = ctx.request.body
  ctx.body = await register({ userName, password, gender })
})

// 用户名是否存在
router.post('/isExist', async (ctx, next) => {
  const { userName } = ctx.request.body

  ctx.body = await isExist(userName)
})

// 登录路由
router.post('/login', async (ctx, next) => {
  const { userName, password } = ctx.request.body
  ctx.body = await login(ctx, userName, password)

})

// 删除路由
router.post('/delete', loginCheck, async (ctx, next) => {
  if (isTest) {
    // 测试环境下，测试账号登录之后，删除自己
    const { userName } = ctx.session.userInfo
    ctx.body = await delCurUser(userName)
  }
})

// 修改个人信息
router.patch('/changeInfo', loginCheck, genValidator(userValidate), async (ctx, next) => {
  const { nickName, city, picture } = ctx.request.body
  ctx.body = await changeInfo(ctx, {
    nickName,
    city,
    picture
  })
})

// 修改密码
router.patch('/changePassword', loginCheck, async (ctx, next) => {
  const { password, newPassword } = ctx.request.body
  const { userName } = ctx.session.userInfo
  ctx.body = await changePassword(userName, password, newPassword)
})

// 退出登录
router.post('/logout', loginCheck, async (ctx, next) => {
  ctx.body = await logout(ctx)
})

router.get('/getAtList', loginCheck, async (ctx, next) => {
  const { id: userId } = ctx.session.userInfo
  const result = await getFollowers(userId)
  const { followersList } = result.data
  const list = followersList.map(user => {
    return `${user.nickName} - ${user.userName}`
  })
  // 格式如 ['张三 - zhangsan', '李四 - lisi', '昵称 - userName']
  ctx.body = list
})

module.exports = router
